package com.wonderful.eyepetizer.ui.pages.person

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ShoppingCart
import androidx.compose.material.icons.outlined.Create
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.wonderful.eyepetizer.R
import com.wonderful.eyepetizer.app.mApplication
import com.wonderful.eyepetizer.ui.theme.Black_26
import com.wonderful.eyepetizer.ui.theme.NormalItemColor

/**
 * Desc: 个人
 * @Author: wonderful
 * @Time: 2024/1/19 16:48
 */

val titles = mutableStateListOf(
    mApplication.getString(R.string.my_message),
    mApplication.getString(R.string.my_record),
    mApplication.getString(R.string.my_cache),
    mApplication.getString(R.string.watch_record),
    mApplication.getString(R.string.my_barrage),
    mApplication.getString(R.string.my_add_chase),
    mApplication.getString(R.string.personalized_attire),
    mApplication.getString(R.string.my_bookshelf),
    mApplication.getString(R.string.my_chicken_leg),
    mApplication.getString(R.string.my_circle),
    mApplication.getString(R.string.my_order),
    mApplication.getString(R.string.my_love),
    mApplication.getString(R.string.study_center),
    mApplication.getString(R.string.feedback),
)

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun PersonPage() {
    LazyColumn(modifier = Modifier.fillMaxSize(), contentPadding = PaddingValues(bottom = 30.dp)) {
        item {
            ProfileHeaderWidget()
        }
        stickyHeader {
            ProfileTabWidget()
        }
        items(titles.size) {
            SettingWidget(text = titles[it])
        }
    }
}

@Composable
fun ProfileHeaderWidget() {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .wrapContentHeight()
    ) {
        Image(
            painter = painterResource(id = R.mipmap.ic_head_bg),
            contentDescription = null,
            modifier = Modifier
                .fillMaxSize()
                .height(200.dp)
                .alpha(0.75f)
                .blur(20.dp),
            contentScale = ContentScale.FillBounds
        )
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .align(Alignment.Center),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Image(
                painter = painterResource(id = R.mipmap.home_ic_img_avatar),
                contentDescription = null,
                modifier = Modifier
                    .size(88.dp)
                    .clip(CircleShape),
                contentScale = ContentScale.Crop
            )
            Text(
                text = stringResource(id = R.string.view_personal_homepage),
                style = TextStyle(fontSize = 12.sp, color = Color.White),
                modifier = Modifier.padding(top = 10.dp)
            )
        }
    }
}

@Composable
fun ProfileTabWidget() {
    Column(
        modifier = Modifier.background(Color.White),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Row(
            modifier = Modifier
                .padding(top = 20.dp)
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceAround,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Row(verticalAlignment = Alignment.CenterVertically) {
                Icon(
                    imageVector = Icons.Default.ShoppingCart,
                    contentDescription = null,
                    tint = Black_26,
                    modifier = Modifier.size(14.dp)
                )
                Text(
                    text = stringResource(id = R.string.collect),
                    style = TextStyle(color = Black_26, fontSize = 15.sp),
                    modifier = Modifier.padding(start = 5.dp)
                )
            }
            Canvas(modifier = Modifier.size(width = 0.5.dp, height = 40.dp), onDraw = {
                drawLine(
                    color = NormalItemColor,
                    start = Offset(0f, 0f),
                    end = Offset(0f, size.height)
                )
            })
            Row(verticalAlignment = Alignment.CenterVertically) {
                Icon(
                    imageVector = Icons.Outlined.Create,
                    contentDescription = null,
                    tint = Black_26,
                    modifier = Modifier.size(14.dp)
                )
                Text(
                    text = stringResource(id = R.string.comment),
                    style = TextStyle(color = Black_26, fontSize = 15.sp),
                    modifier = Modifier.padding(start = 5.dp)
                )
            }
        }
        Canvas(modifier = Modifier
            .padding(top = 20.dp)
            .fillMaxWidth()
            .height(0.5.dp), onDraw = {
            drawLine(
                color = NormalItemColor,
                start = Offset(0f, 0f),
                end = Offset(size.width, size.height)
            )
        })
    }
}

@Composable
fun SettingWidget(text: String) {
    val context = LocalContext.current
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 30.dp)
            .background(Color.White),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = text,
            style = TextStyle(color = Black_26, fontSize = 16.sp),
            modifier = Modifier.clickable {
                when (text) {
                    context.getString(R.string.watch_record) -> {
                        HistoryWatchActivity.actionStart(context)
                    }
                }
            })
    }
}